package com.qfmy.web.app.service.impl.listenBook;

import com.qfmy.common.exception.BaseException;
import com.qfmy.common.login.LoginUser;
import com.qfmy.common.login.LoginUserHolder;
import com.qfmy.common.result.ResultCodeEnum;
import com.qfmy.model.entity.listenBook.PlayRecord;
import com.qfmy.model.vo.listenBook.PlayRecordVo;
import com.qfmy.web.app.mapper.listenBook.PlayRecordMapper;
import com.qfmy.web.app.service.listenBook.PlayRecordService;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.Nullable;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 清风明月
 * @Package com.qfmy.web.app.service.impl.listenBook
 * @date 2025/11/01
 * @description 播放历史
 */
@Service
public class PlayRecordServiceImpl implements PlayRecordService {

    /**
     * 注入mapper
     */
    @Autowired
    private PlayRecordMapper playRecordMapper;

    /**
     * 添加播放记录
     * @param playRecord
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void savePlayRecord(PlayRecord playRecord) {
        //获取用户id
        LoginUser loginUser = LoginUserHolder.getLoginUser();
        //判断用户是否登录
        if (loginUser == null) {
            throw new BaseException(ResultCodeEnum.TOKEN_INVALID);
        }
        //获取用户id
        Long userId = loginUser.getUserId();
        if(userId == null || userId < 0)
        {
           return;
        }
        //获取小说id
        Long audioId = playRecord.getAudioId();
        //根据用户id和小说id查询播放记录
        PlayRecord playRecordByAudioId = playRecordMapper.getPlayRecordByAudioId(Math.toIntExact(audioId), userId);
        //判断播放记录是否存在
        if(playRecordByAudioId != null)
        {
            //更新播放记录
            playRecordMapper.updatePlayRecord(playRecord);
        }else{
            //添加播放记录
            playRecordMapper.savePlayRecord(playRecord);
        }
    }

    /**
     * 删除播放记录
     * @param playRecordId
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deletePlayRecord(Integer playRecordId) {
        playRecordMapper.deletePlayRecord(playRecordId);
    }

    /**
     * 获取播放记录
     * @return
     */
    @Override
    public List< PlayRecordVo> getPlayRecord() {
        //获取用户id
        LoginUser loginUser = LoginUserHolder.getLoginUser();
        //判断用户是否登录
        if (loginUser == null) {
            throw new BaseException(ResultCodeEnum.TOKEN_INVALID);
        }
        Long userId = loginUser.getUserId();
        //校验
        if(userId == null || userId < 0)
        {
            return Collections.emptyList();
        }
        //根据用户id查询所有播放记录
        List<PlayRecord> playRecordList = playRecordMapper.getPlayRecord(userId);
        //转换vo
        return convertToVo(playRecordList);
    }

    /**
     * 根据音频id查询播放记录
     * @param audioId
     * @return
     */
    @Override
    public PlayRecordVo getPlayRecordByAudioId(Integer audioId) {
        //获取用户id
        LoginUser loginUser = LoginUserHolder.getLoginUser();
        //判断用户是否登录
        if (loginUser == null) {
            throw new BaseException(ResultCodeEnum.TOKEN_INVALID);
        }
        Long userId = loginUser.getUserId();
        //校验
        if(userId == null || userId < 0)
        {
            return null;
        }
        //根据用户id和音频id查询播放记录
        PlayRecord playRecord = playRecordMapper.getPlayRecordByAudioId(audioId, userId);
        //判断播放记录是否存在
        if (playRecord == null) {
            return null;
        }
        //进行对象的转换
        PlayRecordVo playRecordVo = new PlayRecordVo();
        BeanUtils.copyProperties(playRecord, playRecordVo);
       return playRecordVo;
    }

    /**
     * 清空所有播放记录
     */
    @Override
    public void clearAllPlayRecord() {
        //获取用户id
        LoginUser loginUser = LoginUserHolder.getLoginUser();
        //判断用户是否登录
        if (loginUser == null) {
            throw new BaseException(ResultCodeEnum.TOKEN_INVALID);
        }
        Long userId = loginUser.getUserId();
        //判断用户id
        if (userId == null || userId < 0)
        {
            return;
        }
        //清空所有播放记录
        playRecordMapper.clearAllPlayRecord(userId);
    }

    /**
     * 转换vo
     * @param playRecordList
     * @return
     */
    @Nullable
    @Contract(pure = true)
    private List<PlayRecordVo> convertToVo(List<PlayRecord> playRecordList) {
        //判断集合是否为空
        if (playRecordList != null && !playRecordList.isEmpty()) {
            return playRecordList.stream().map(playRecord -> {
                PlayRecordVo playRecordVo = new PlayRecordVo();
                BeanUtils.copyProperties(playRecord, playRecordVo);
                return playRecordVo;
            }).collect(Collectors.toList());
        }
        return Collections.emptyList();
    }
}
